package com.hero.content.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hero.content.mapper.TeachplanMapper;
import com.hero.content.model.dto.BindTeachplanMediaDto;
import com.hero.content.model.dto.TeachplanDto;
import com.hero.content.model.pojo.Teachplan;
import com.hero.content.model.pojo.TeachplanMedia;
import com.hero.content.service.TeachplanMediaService;
import com.hero.content.service.TeachplanService;
import com.hero.execption.XueChengPlusException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private TeachplanMediaService mediaService;

    /**
     * 根据课程id查询课程计划
     *
     * @param cid
     * @return
     */
    @Override
    public List<Teachplan> selectTeachplanList(Long cid) {
        if (ObjUtil.isNull(cid)) XueChengPlusException.cast("课程id不能为空");
        return teachplanMapper.selectTeachPlanList(cid);
    }

    /**
     * 添加/修改课程计划
     *
     * @param teachplan
     */
    @Override
    public void addTeachplan(Teachplan teachplan) {
        // 先对数据做校验
        if (!ObjUtil.isAllNotEmpty(teachplan, teachplan.getCourseId(), teachplan.getParentid()
                , teachplan.getGrade(), teachplan.getPname())) {
            XueChengPlusException.cast("参数不能为空");
        }

        // 修改
        if (ObjUtil.isNotEmpty(teachplan.getId())) {
            Teachplan teachplan1 = this.getById(teachplan.getId());
            BeanUtil.copyProperties(teachplan, teachplan1, false);
            this.updateById(teachplan1);
        } else {
            // 给数据做默认值填充
            teachplan.setMediaType("1");

            // 根据课程id和parentid统计
            // 然后把统计结果 +1 设置为排序编号

            int count = this.count(Wrappers.lambdaQuery(Teachplan.class)
                    .eq(Teachplan::getCourseId, teachplan.getCourseId())
                    .eq(Teachplan::getParentid, teachplan.getParentid())
            );
            teachplan.setOrderby(count + 1);
            teachplan.setCoursePubId(203001L);
            teachplan.setStatus(1);
            teachplan.setCreateDate(LocalDateTime.now());
            teachplan.setChangeDate(LocalDateTime.now());

            // 保存数据到数据库中
            this.save(teachplan);
        }
    }

    /**
     * 给课程计划绑定视频资源
     *
     * @param bindTeachplanMediaDto
     */
    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        if (!ObjUtil.isAllNotEmpty(bindTeachplanMediaDto, bindTeachplanMediaDto.getTeachplanId(),
                bindTeachplanMediaDto.getMediaId(), bindTeachplanMediaDto.getFileName()))
            XueChengPlusException.cast("参数不能为空");
        // 获取课程id
        Teachplan teachplan = teachplanMapper.selectById(bindTeachplanMediaDto.getTeachplanId());
        if (ObjUtil.isEmpty(teachplan)) XueChengPlusException.cast("课程对象不存在");
        // 先判断这个课程计划是否已经绑定过视频 如果有 先删除
        mediaService.remove(Wrappers.lambdaQuery(TeachplanMedia.class)
                .eq(TeachplanMedia::getTeachplanId, bindTeachplanMediaDto.getTeachplanId()));
        // 绑定视频的只能是小节 不能是大章节
        if (teachplan.getGrade() <= 1) XueChengPlusException.cast("只能给小节绑定视频");
        // 拼凑课程计划和资源中间表实体对象
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setTeachplanId(bindTeachplanMediaDto.getTeachplanId());
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        // 保存数据到数据库中
        mediaService.save(teachplanMedia);
    }

    /**
     * 接触视频资源和课程计划的关系
     *
     * @param teachPlanId
     * @param mediaId
     */
    @Override
    public void delAassociationMedia(Long teachPlanId, String mediaId) {
        if (!ObjUtil.isAllNotEmpty(teachPlanId, mediaId)) XueChengPlusException.cast("参数不能为空");
        mediaService.remove(Wrappers.lambdaQuery(TeachplanMedia.class)
                .eq(TeachplanMedia::getTeachplanId, teachPlanId)
                .eq(TeachplanMedia::getMediaId, mediaId)
        );
    }

    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }
}
